package com.CGFrame.struts.bean.message;

import java.util.Iterator;
import java.util.List;

import com.CGFrame.struts.POJOs.Sector;
import com.CGFrame.struts.bean.ProjectOperate;
import com.CGFrame.struts.bean.ProjectOperateFactory;

public class TreeviewRender {
	static public String renderStatic(TreeViewElement treeElmt, boolean bHidden) {
		StringBuffer content = new StringBuffer();
		content.append("<li id=" + treeElmt.getID() + ">");//��ʾliԪ��
		if (bHidden && treeElmt.canExpand())//������չ�����ҳ�ʼ��Ϊ����,����ʾչ��ͼƬ�������showHide�¼�
			content.append("<img src=images/plus.gif onClick=\"showHide('"
					+ treeElmt.getID() + "')\">");
		else if (treeElmt.canExpand())//������չ��,����ʾ�۵�ͼƬ�������showHide�¼�
			content.append("<img src=images/minus.gif onClick=\"showHide('"
					+ treeElmt.getID() + "')\">");
		else//������ʾ�հ�ͼƬ
			content.append("<img src=images/blank.gif>");
		content.append("<img src=images/folder.gif><a ");//����ļ���ͼƬ
//		if (treeElmt.canExpand())//������չ����Ϊ�ڵ����showHide�¼�
//			content.append("onClick=\"showHide('" + treeElmt.getID() + "')\"");
		content.append(">" + treeElmt.getNodeName() + "</a>");
		if (treeElmt.canExpand()) {//������չ��������UL�����ʾ�ӽ�� 
			content.append("<ul");
			if (bHidden)
				content.append(" style=\"display:none;\" ");
			content.append(">");
			TreeViewElement[] elmts = treeElmt.getChildren();
			for (int i = 0; i < elmts.length; i++)
				content.append(renderStatic(elmts[i], bHidden));//�ݹ������ʾ�ӽڵ�
			content.append("</ul>");
		}
		content.append("</li>");//������ʾ<li>Ԫ��
		return content.toString();
	}

	static public TreeViewElement[] getTopCategory() {
		ProjectOperate pop=ProjectOperateFactory.getprojectOperate();
		List list=pop.Query("Sector", "parentId,"+"0", null, null);
		
		if(list.isEmpty()){
			return null;
		}
		SectorForTreeView[] tve=new SectorForTreeView[list.size()];
		Sector sector;
		int i=0;
		for(Iterator iter=list.iterator();iter.hasNext();){
			sector=(Sector)iter.next();
			tve[i]=new SectorForTreeView();
			tve[i].setId(sector.getSectorId());
			tve[i].setCompanyId(sector.getCompany().getCompanyId());
//			tve[i].setLeaderId(sector.getLeaderId());
			tve[i].setName(sector.getsectorName());
			tve[i].setParentId(sector.getParentId());
			i++;
		}
		return tve;
	}
	static public String renderSimple(TreeViewElement treeElmt) {
		StringBuffer content = new StringBuffer();
		content.append("<li id=" + treeElmt.getID() + "><a href=\"#\">");
		content.append( treeElmt.getNodeName() + "</a>");
		if (treeElmt.canExpand()) {
			content.append("<ul>");
			TreeViewElement[] elmts = treeElmt.getChildren();
			for (int i = 0; i < elmts.length; i++)
				content.append(renderSimple(elmts[i]));
			content.append("</ul>");
		}
		content.append("</li>");
		return content.toString();
	}
	static public String renderTreeViewAjax(TreeViewElement treeElmt,boolean bRenderChildren) {
		StringBuffer content = new StringBuffer();
		content.append("<li id="+treeElmt.getID()+">");
	    if (treeElmt.canExpand())
			content.append("<img src=images/plus.gif onClick=\"getSubTree('"+treeElmt.getID()+"')\">");
		else
			content.append("<img src=images/blank.gif>");
		content.append("<img src=images/folder.gif><a href=\"#\"");
		if (treeElmt.canExpand())
		  content.append(" onClick=\"getSubTree('"+treeElmt.getID()+"')\"");
		content.append(">"+treeElmt.getNodeName()+"</a>");
		
		if ( bRenderChildren&&treeElmt.canExpand( ))
		{
			content.append("<ul>");
			TreeViewElement[] elmts = treeElmt.getChildren();
			for (int i=0;i<elmts.length;i++)
				content.append(renderTreeViewAjax(elmts[i],false));
			content.append("</ul>");
		}
		
		content.append("</li>");
		return content.toString();
	}
}
